package zhizhemodi.tech.short_term_bwork.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import zhizhemodi.tech.short_term_bwork.entity.Device;
import zhizhemodi.tech.short_term_bwork.mapper.DeviceMapper;
import zhizhemodi.tech.short_term_bwork.service.DeviceService;
import zhizhemodi.tech.short_term_bwork.tool.CommonResult;

@RestController
@RequestMapping("device")
public class DeviceController {
    
    // 默认每页条数
    @Value("${default.page.rowNum}")
    private Integer ROW_MAX;

    @Autowired
    DeviceMapper deviceMapper;

    // 设备服务
    @Resource
    DeviceService deviceService;

    /**
     * 获取设备列表
     * @param departmentId
     * @param rowNum
     * @param pageNum
     * @return
     */
    @GetMapping("list")
    public CommonResult<?> getDeviceList(
        @RequestParam("departmentId") Integer departmentId,
        @RequestParam(required = false, name = "limit") Integer rowNum,
        @RequestParam(required = false, name = "page") Integer pageNum
    ) {
        List<Device> dList = new ArrayList<Device>();
        try{
            QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("department_id", departmentId);
            // 分页操作
            String last = null;
            if(pageNum == null) pageNum = 0;
            // 计算页数偏移量
            if(rowNum != null){
                last = " limit " + pageNum * rowNum + "," + rowNum; 
            }
            else {
                last = " limit " + pageNum * ROW_MAX + "," + ROW_MAX;
            }
            queryWrapper.last(last);
            dList = deviceMapper.selectList(queryWrapper);
        } catch(Exception e) {
            return CommonResult.failed(e.getMessage());
        }
        Map<Object, Object> re = new HashMap<>();
        re.put("list", dList);
        re.put("page", pageNum);
        return CommonResult.success(re);
    }

    /**
     * 新增设备
     * @param dList
     * @return
     */
    @PostMapping("add")
    public CommonResult<?> addDevice(
        @RequestBody List<Device> dList
    ) {
        try{
            List<Device> now = deviceMapper.selectList(null);
            Integer next_id = -1;
            if(now.size() > 0) next_id = now.get(now.size() - 1).getDeviceId();
            for(Device d : dList){
                QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("MAC_Addr", d.getMacAddr());
                if(!deviceMapper.exists(queryWrapper)){
                    next_id+=1;
                    d.setDeviceId(next_id);
                    deviceMapper.insert(d);
                }
            } 
        }catch(Exception e) {
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success("新增设备成功");
    }

    /**
     * 更新设备
     * @param changeBodyMap
     * @param dId
     * @return
     */
    @PutMapping("update")
    public CommonResult<?> updateDevice(
        @RequestBody Map<String, String> changeBodyMap,
        @RequestParam("deviceId") Integer dId
    ) {
        try {
            QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("device_id", dId);
            Device device = deviceMapper.selectOne(queryWrapper);
            System.out.println(Integer.parseInt(changeBodyMap.get("departmentId")));
            if(changeBodyMap.get("deviceName") != null) device.setDeviceName(changeBodyMap.get("deviceName"));
            if(changeBodyMap.get("departmentId") != null) device.setDepartmentId(Integer.parseInt(changeBodyMap.get("departmentId")));
            deviceMapper.updateById(device);
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success("修改成功");
    }


    /**
     * 删除设备
     * @param dId
     * @return
     */
    @DeleteMapping("delete")
    public CommonResult<?> deleteDevice( @RequestParam("deviceId") Integer dId) {
        try {
            deviceService.deleteDevices(dId);
        } catch(Exception e) {
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success("删除成功");
    }
}
